package com.mdd.common.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.basics.IBaseMapper;
import com.mdd.common.entity.PushInfo;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.ToolUtils;
import com.mdd.common.validate.push.PushInfoListSearchValidate;
import com.mdd.common.validate.push.PushInfoSearchValidate;
import com.mdd.common.vo.push.PushInfoListedVo;
import com.mdd.common.vo.push.PushInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 发布信息Mapper
 *
 * @author LikeAdmin
 */
@Mapper
public interface PushInfoMapper extends IBaseMapper<PushInfo> {

    default String randMakeOrderSn(String field) {
        String date = TimeUtils.timestampToDate(System.currentTimeMillis() / 1000, "yyyyMMddHHmmss");
        String sn;
        while (true) {
            sn = date + ToolUtils.randomInt(6);
            PushInfo snModel = this.selectOne(
                    new QueryWrapper<PushInfo>()
                            .select("id")
                            .eq(field, sn)
                            .last("limit 1"));
            if (snModel == null) {
                break;
            }
        }
        return sn;
    }

    /**
     * 发布列表
     *
     * @param page
     * @param searchValidate
     * @return
     */
    IPage<PushInfoListedVo> getListPc(Page page, @Param("req") PushInfoSearchValidate searchValidate);


    /**
     * 获取发布
     *
     * @param page
     * @param req
     * @return
     */
    IPage<PushInfoVo> getPushInfoList(Page page, @Param("req") PushInfoListSearchValidate req);

}
